//
//  WZPAdvertViewController.m
//  WZPItem
//
//  Created by 王王喆平 on 16/7/7.
//  Copyright © 2016年 UI. All rights reserved.
//

#import "WZPAdvertViewController.h"
#import <AFNetworking.h>
#import "WZPItem.h"
#import <MJExtension/MJExtension.h>
#import <UIImageView+WebCache.h>
#import "WZPTableController.h"

#define WZPXcode @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"
#define iPhone6P (WZPScreenH == 736)
#define iPhone6  (WZPScreenH == 667)
#define iPhone5  (WZPScreenH == 568)
#define iPhone4  (WZPScreenH == 480)

@interface WZPAdvertViewController ()
@property (strong,nonatomic) WZPItem * item ;
@property (weak,nonatomic) NSTimer * timer;
@property (weak, nonatomic) IBOutlet UIButton *jumBtn;
@property (weak, nonatomic) IBOutlet UIView *conView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;


@end
@implementation WZPAdvertViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    
    [self setupLanunchimage];
    
    [self loadData];
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
}
- (IBAction)jumBtn:(id)sender {
    // 点击跳过进入主界面
    WZPTableController * tabVc = [[WZPTableController alloc]init];
    [UIApplication sharedApplication].keyWindow.rootViewController = tabVc;
    
    // 进入主界面后销毁定时器
    [_timer invalidate];
}
// 设置跳过时间
-(void)timeChange{
    static int i = 3;
    i--;
    if (i == 0) {
        // 计时结束
        [self jumBtn:nil];
    }
 // 设置标题按钮
    NSString * titing = [NSString stringWithFormat:@"跳过(%d)",i];
    [_jumBtn setTitle:titing forState:UIControlStateNormal];
}
// 加载广告数据
-(void)loadData{
    // 创建会话管理者
    AFHTTPSessionManager * mer = [AFHTTPSessionManager manager];
    // 管理请求,响应
    mer.responseSerializer = [AFJSONResponseSerializer serializer];
    mer.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    // 拼接请求参数
    NSMutableDictionary * mudict = [NSMutableDictionary dictionary];
    mudict[@"code2"] = WZPXcode;
    // 发送请求
    [mer GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:mudict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary * adDict = [responseObject[@"ad"] firstObject];
        // 字典转模型
        WZPItem * item = [WZPItem mj_objectWithKeyValues:adDict];
        _item = item;
        // 将数据展示到界面上
        UIImageView * adView = [[UIImageView alloc]init];
        adView.userInteractionEnabled = YES;
        // 添加点按手势
        UITapGestureRecognizer * tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
        [adView addGestureRecognizer:tapGes];
        [_conView addSubview:adView];
        
        [adView sd_setImageWithURL:[NSURL URLWithString:item.w_picurl]];
        CGFloat w = WZPScreenW;
        if (item.w <= 0) return;
        CGFloat h = w/item.w * item.h;
        adView.frame = CGRectMake(0, 0, w, h);
        // 写成plist 文件
//    [responseObject writeToFile:@"/Users/wangwangzheping/Desktop/项目/01/01-题目/wzp.plist" atomically:YES];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}
-(void)tap{
    // 点击广告跳转到广告
    NSURL * url =[NSURL URLWithString:_item.ori_curl];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }

}
-(void)setupLanunchimage{
    // 进行屏幕适配
    UIImage *image = nil;
    if (iPhone6P) {
        image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
        
    } else if(iPhone6){
        image = [UIImage imageNamed:@"LaunchImage-800-667h"];
        
    }else if (iPhone5){
        image = [UIImage imageNamed:@"LaunchImage-568h"];
        
    }else if (iPhone4){
        image = [UIImage imageNamed:@"LaunchImage"];
    
    }
    _imageView.image = image;

}






@end
